<?php
/**
 * 用户登录注册
 * @author  jackzheng
 */
 
include_once(GZ_PATH . '/includes/user_func.php');
 
class userAction extends Action{
    /**我的-个人信息
     * @author leiying
     */
    public function get_userinfo(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $user = get_userInfo($uid);

        GZ_Api::outPut($user);

    }

    /**编辑个人信息
     * @author leiying
     */
    public function edit_userinfo(){
        GZ_Api::authToken();
        $uid = _POST("user_id");
        $data["headimg"] = _POST("headimg","");
        $data["nickname"] = _POST("nickname","");
        $data["sex"] = _POST("sex","");
        $data["birthday"] = _POST("birthday","0000-00-00");
        $sql = "SELECT `headimg` FROM ".$this->table("users")." WHERE `user_id`=".$uid;
        $old_headimg = $this->getOne($sql);

        $res = $this->autoExecute($this->table('users'), $data, 'UPDATE', "user_id = ".$uid);
        if($res){
            if($old_headimg != $data["headimg"]){
                @unlink($_SERVER['DOCUMENT_ROOT'].$old_headimg);
            }
            GZ_API::message("编辑成功",1);
        }else{
            GZ_API::message("编辑失败",2);
        }
    }
    /**
     * 
     * @author
    */
    public function login(){
		
	 	$user_name	 = _POST('user_phone','');
		$user_pwd	 = _POST('user_pwd','');
		
		$res = app_login($user_name, $user_pwd);
		
		if($res){
			
			GZ_Api::get_accesskey();
			$res_data = get_userInfo($res);
			$res_data['access_id'] = ACCESS_ID;
			
			//判断用户账号状态
			if($res_data['status'] != 1){
                GZ_API::outPut(11);
            }
			
            //更新用户TOKEN
			$access_id = update_token(ACCESS_ID,$res_data);
			GZ_Api::outPut($res_data);
			
		}else{
			
			GZ_Api::outPut(6);	
		}
		
    }
	
	
	/**  
     * 注册
    */
	public function register(){
		
	    //参数处理
        $input_data['user_phone'] = _POST('account','');//主账号
		$input_data['password']	 = _POST('password','');
		$input_data['code']		 = _POST('code',''); 

		
        if(empty($input_data['user_phone'])){GZ_API::message("用户账号不能为空",2);}
		if(empty($input_data['password'])){GZ_API::message("用户密码不能为空",2);}
		//if(empty($input_data['code'])){GZ_API::message("短信验证码不能为空",2);}
		
		//会员注册
        $res = app_register($input_data);
		
		if($res['code'] == 0){
			GZ_API::message($res['info'],2);
		}else{
			GZ_API::message($res['info'],1);
		}
		
    }
	
	
	/**
	* 修改密码(忘记密码)
	*/
	public function update_pwd(){
		
		$user_mobile = _POST("mobile", "");
		$sms_code = _POST("code", "");
		$new_password = _POST("new_pwd", "");
		
		if(trim($user_mobile) == ''){GZ_API::message("手机号不能为空",2);}
        if($new_password == ''){GZ_API::message("新密码不能为空",2);}
		
		//=================检查用户是否存在=================================
   		$sql = "SELECT user_id FROM " . $GLOBALS['db']->table("users") .
		" WHERE user_phone='$user_mobile'";
   		$res_uid = $this->getOne($sql);
		
		if(empty($res_uid)){
			GZ_API::message("手机账号不存在",0);
		}
		
		
		//===================验证短信码====================================
		$sql = 'SELECT record_code FROM ' .$this->table('validate_record').
				' WHERE record_key ="'.$user_mobile.'"';
		$res_record = $this->getOne($sql);
		
		if(!empty($res_record)){
			if($res_record != $sms_code){
				GZ_API::message("短信验证码不正确",2);
			}
		}else{
			GZ_API::message("验证码信息不存在",2);
		}
   
        $where = ' user_id='.$res_uid;
        $user_data['key_salt'] = "";
		$user_data['password'] = $new_password;
        $res = $this->autoExecute($this->table('users'), $user_data, 'UPDATA',$where);
        if($res){
            GZ_API::message("修改密码成功",1);
        }else{
            GZ_API::message("修改密码失败",0);
        }
	}
	
	
	/**
	* 修改密码(个人中心更新密码)
	*/
	public function set_pwd(){
		
		GZ_Api::authToken();
		$uid = intval(_POST("user_id", 0));
		$old_pwd = _POST("old_pwd", "");
		$new_password = _POST("new_pwd", "");
		
		if($old_pwd == ''){GZ_API::message("旧密码不能为空",2);}
        if($new_password == ''){GZ_API::message("新密码不能为空",2);}
		
		//=================检查用户密码=================================
   		$sql = "SELECT user_id,password,key_salt FROM " . $GLOBALS['db']->table("users") .
		" WHERE user_id='$uid'";
   		$res_info = $this->getRow($sql);
		
		$user_pwd = md5(md5($old_pwd). $res_info['key_salt']);
		
		if(empty($res_info['key_salt'])){
			$user_pwd = md5($old_pwd);
		}
		
		if($user_pwd != $res_info['password']){
			GZ_API::message("旧密码不正确",0);
		}
   
        $where = ' user_id='.$uid;
        $user_data['key_salt'] = "";
		$user_data['password'] = $new_password;
        $res = $this->autoExecute($this->table('users'), $user_data, 'UPDATA',$where);
        if($res){
            GZ_API::message("修改密码成功",1);
        }else{
            GZ_API::message("修改密码失败",0);
        }
	}
	
	/**
     *  实名认证
     * 
     */
	public function audit(){
		GZ_Api::authToken();
		$user_id = _POST("user_id", 0);
		$user_name = _POST("user_name", "");
		$id_card = _POST("id_card", "");
		
		if(empty($user_name)){GZ_API::message("真实姓名不能为空",2);}
		if(empty($id_card)){GZ_API::message("身份证号码不能为空",2);}
		
		
		$user_auth['uid'] = $user_id;
		$user_auth['auth_name'] = $user_name;
		$user_auth['identity_card'] = $id_card;
		$user_auth['state'] = 0;
		$user_auth['add_time'] = time();
		
		//INSERT
		$res = $this->autoExecute($GLOBALS['db']->table('user_auth'), $user_auth, 'INSERT');
		
		if($res){
            GZ_API::message("提交实名认证成功",1);
        }else{
            GZ_API::message("提交实名认证失败",0);
        }
		
		
	}
	
	
	
	
    /**
     * 
     * @author 
     */
    public function get_token(){
	
		$res =  get_all_token();
       	GZ_Api::outPut($res);
    }
	
	/**
     * 上传图片
     * @author 
     */
	public function upload_img(){
		
		GZ_Api::authToken();
		
		$user_id = _POST('user_id');//用户ID;
		$type = _POST('type',0);
		if($type == 0){
		    GZ_Api::message("请选择上传图片类型",2);
        }
		upload_imgae($type);

		
	}





}

?>